<?php

namespace wechat\src;

/**
 * 微信公式操作类
 */
use wechat\Wechat;

class Common extends SrcAbstract {

    /**
     * 获取全局唯一接口调用凭据
     */
    public function AccessToken() {
        $baseObject = $this->wechat->base();
        $accessToken = $baseObject->getCache('access_token');
        if (empty($accessToken)) {
            $url = "https://api.weixin.qq.com/cgi-bin/token";
            $params = [
                'appid' => $this->wechat->appid,
                'secret' => $this->wechat->appsecret,
                'grant_type' => 'client_credential',
            ];
            $result = HttpRequest($url, $params, false);
            if ($result === false) {
                $this->setError(102, "取得凭证错误");
                return false;
            } else {
                $resultArray = json_decode($result, true);
                if (empty($resultArray['errcode']) || $resultArray['errcode'] == 0) {
                    $baseObject->setCache('access_token', $resultArray['access_token'], $resultArray['expires_in']);
                    return $resultArray['access_token'];
                } else {
                    $this->setError(102, $resultArray['errcode'] . ":" . $resultArray['errmsg']);
                    return false;
                }
            }
        }
        return $accessToken;
    }
    
    public function getUserinfo() {
        
    }
}
